package cn.oveay.classdesign.dao.impl;

import cn.oveay.classdesign.bean.basebean.Subject;
import cn.oveay.classdesign.dao.SubjectDao;
import cn.oveay.classdesign.util.jdbc.TxQueryRunner;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class SubjectDaoImpl implements SubjectDao {
    private QueryRunner qr = new TxQueryRunner();

    @Override
    public List<Subject> findAll() {
        String sql = "select * from t_subject";
        try {
            return qr.query(sql, new BeanListHandler<>(Subject.class));
        } catch (SQLException e) {
            throw new RuntimeException("SubjectDao Error. " + e.getMessage());
        }
    }

    @Override
    public List<Subject> findByUserId(String userId) {
        String sql = "select * from t_subject where userId=?";
        try {
            return qr.query(sql, new BeanListHandler<>(Subject.class), userId);
        } catch (SQLException e) {
            throw new RuntimeException("SubjectDao Error. " + e.getMessage());
        }
    }

    @Override
    public Subject findById(String id) {
        String sql = "select * from t_subject where id=?";
        try {
            return qr.query(sql, new BeanHandler<>(Subject.class), id);
        } catch (SQLException e) {
            throw new RuntimeException("SubjectDao Error. " + e.getMessage());
        }
    }

    @Override
    public List<Subject> findByKey(String key) {
        String sql = "select * from t_subject where title like ?";
        try {
            return qr.query(sql, new BeanListHandler<>(Subject.class), "%" + key + "%");
        } catch (SQLException e) {
            throw new RuntimeException("SubjectDao Error. " + e.getMessage());
        }
    }

    @Override
    public void inseart(Subject option) {
        String sql = "insert into t_subject values(?,?,?,?,?,?)";
        Object[] params = {option.getId(), option.getTitle(), option.getNumber(), option.getStarttime(), option.getEndtime(), option.getUserId()};
        try {
            qr.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException("SubjectDao Error." + e.getMessage());
        }
    }

    @Override
    public void update(Subject subject) {
        String sql = "update t_subject set title=?, starttime=?, endtime=? where id=?";
        try {
            qr.update(sql, subject.getTitle(), subject.getStarttime(), subject.getEndtime(), subject.getId());
        } catch (SQLException e) {
            throw new RuntimeException("SubjectDao Error." + e.getMessage());
        }

    }

    @Override
    public void delete(String id) {
        String sql = "delete from t_subject where id=?";
        try {
            qr.update(sql, id);
        } catch (SQLException e) {
            throw new RuntimeException("SubjectDao Error." + e.getMessage());
        }
    }
}
